<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>公司信息管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <h2>公司列表 <i class="el-icon-office-building"></i></h2><br/>
    <el-button type="primary" @click="add" class="mr-5 mb-5">新 增 <i class="el-icon-circle-plus-outline"></i>
    </el-button>
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item v-for="company in companies">
        <template slot="title">
          <h3 style="color:#409EFF">{{ company.companyName }} <i class="el-icon-info"></i></h3>
        </template>
        <el-descriptions class="margin-top" title="详细信息" :column="3" size="medium" border>
          <template slot="extra">
            <el-button type="success" @click="edit(company)" class="mr-5">修改<i class="el-icon-edit"></i>
            </el-button>
            <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='再想想'
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @confirm="del(company.companyId)">
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              公司id
            </template>
            {{ company.companyId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-mobile-phone"></i>
              联系方式
            </template>
            {{ company.companyPhone }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              所属城市
            </template>
            {{ company.companyCity }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-message"></i>
              公司传真
            </template>
            {{ company.companyFax }}
          </el-descriptions-item>

          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              公司地址
            </template>
            {{ company.companyAdress }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-tickets"></i>
              备注
            </template>
            {{ company.companyRemark }}
          </el-descriptions-item>
        </el-descriptions>

      </el-collapse-item>
    </el-collapse>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form label-width="80px" :model="company" :rules="rules" ref="company">
        <el-form-item label="公司ID">
          <el-input v-model="company.companyId" disabled></el-input>
        </el-form-item>
        <el-form-item label="公司名称">
          <el-input v-model="company.companyName"></el-input>
        </el-form-item>
        <el-form-item label="所属城市">
          <el-input v-model="company.companyCity"></el-input>
        </el-form-item>
        <el-form-item label="公司电话">
          <el-input v-model="company.companyPhone"></el-input>
        </el-form-item>
        <el-form-item label="公司传真">
          <el-input v-model="company.companyFax"></el-input>
        </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-cascader
                style="width: 185px"
                :options="options"
                v-model="areaCodes"
                @change="getArea">
            </el-cascader>
          </el-form-item>
        <el-form-item label="公司备注">
          <el-input v-model="company.companyRemark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save('company')">{{ btnText }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import {CodeToText, regionData, TextToCode} from "element-china-area-data";

export default {
  name: "Company",
  data() {
    return {
      activeName: '1',
      company:{},
      companies: [],
      dialogVisible: false,
      btnText:"",
      options: regionData,
      areaCodes:[],
      rules:{},
    };
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/company/getall").then(res => {
        this.companies = res.data
      })
    },
    edit(company) {
      this.areaCodes = []
      this.company = Object.assign({}, company)
      this.btnText="修改"
      this.dialogVisible = true
      let areaNames=this.company.companyAdress.split('/')
      console.log(this.areaCodes)
      this.areaCodes[0]=TextToCode[areaNames[0]].code
      this.areaCodes[1]=TextToCode[areaNames[0]][areaNames[1]].code
      this.areaCodes[2]=TextToCode[areaNames[0]][areaNames[1]][areaNames[2]].code
    },
    add() {
      this.company={}
      this.btnText="添加"
      this.dialogVisible = true
    },
    save(company) {
      this.$refs[company].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          if (this.btnText==='添加'){
            request.post("company/add", this.company).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }else{
            request.put("company/update", this.company).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    del(id) {
      request.delete("company/delmore/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    getArea(area){
      this.company.companyAdress=this.areatoString(area)
      console.log(area)
    },
    areatoString(value){
      let dz = CodeToText[value[0]] + '/' + CodeToText[value[1]] + '/' + CodeToText[value[2]]
      return dz
    },
  }
}
</script>

<style scoped>

</style>